//
//  RecommendCycleView.swift
//  DouYuZB
//
//  Created by DSDL on 2016/11/1.
//  Copyright © 2016年 鼎商动力. All rights reserved.
//

import UIKit
private let  KCycleCellID  = "KCycleCellID"
class RecommendCycleView: UIView {
    //MARK: - 定义属性
    var  cycleTimer:Timer?
    
    var cycleModel:[CycleModel]?{
        didSet{
            //1.刷新collectionview
            collectionView.reloadData()
            //2.设置pageComtrol的个数
            pageControl.numberOfPages = cycleModel?.count ?? 0
            //3.默认滚动到中间位置
            let indexPath = IndexPath(item: (cycleModel?.count ?? 0)*50, section: 0)
            collectionView.scrollToItem(at: indexPath, at: .left, animated: false)
             //4.添加定时器
            removeCycleTimer()
            addCycleTimer()
           
        }
    }
    
    //MARK: - 控件属性
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var pageControl: UIPageControl!
    
    //MARK: - 系统回调函数
    override func awakeFromNib() {
        super.awakeFromNib()
        //1.设置该控件不随着父控件的拉伸而拉伸
        autoresizingMask = UIViewAutoresizing()
        //2.注册cell
        collectionView.register(CollectionViewCycleCell.NibObject(), forCellWithReuseIdentifier: KCycleCellID)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //1.设置布局
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = collectionView.bounds.size
        
    }
}
//MARK: - UICollectionViewDataSource,UICollectionViewDelegate
extension RecommendCycleView: UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return (cycleModel?.count ?? 0) * 10000
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KCycleCellID, for: indexPath) as! CollectionViewCycleCell
        cell.cycleModel = cycleModel![(indexPath as NSIndexPath).item % (cycleModel?.count)!]

        return cell
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //计算pagecontrol
        let offsetX = scrollView.contentOffset.x + scrollView.bounds.width * 0.5
        
        pageControl.currentPage = Int(offsetX / scrollView.bounds.width) % (cycleModel?.count ?? 1)
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        removeCycleTimer()
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        addCycleTimer()
    }
}
//MARK: - 对定时器的操作
extension  RecommendCycleView {
    //添加定时器
   fileprivate func addCycleTimer()  {
    cycleTimer =  Timer(timeInterval: 3.0, target: self, selector: #selector(scrollToNext), userInfo: nil, repeats: true)
    RunLoop.main.add(cycleTimer!, forMode: RunLoopMode.commonModes)
    }
    //移除定时器
    fileprivate func removeCycleTimer(){
        cycleTimer?.invalidate()
        cycleTimer = nil
    }
    //自定滚动的方法
    @objc fileprivate func scrollToNext(){
        //1.获取滚动的偏移量
        let currentOffsetX = collectionView.contentOffset.x
        let offsetX = currentOffsetX + collectionView.bounds.width
        
        //2.滚动到该位置
        collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
    }
}
